#!/usr/bin/env python 
# -*- coding:utf-8 -*-
"""
@Time:2021/12/26 9:51 下午"
@Author:lydia_liu"
@File:base.py
@function:
"""
import logging

from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException


class BasePage:
    def __init__(self,driver:WebDriver):
        self.driver = driver

    def find(self,by,locator):
        """
        查找到元素
        :param by:
        :param locator:
        :return:
        """
        return self.driver.find_element(by,locator)

    def find_click(self,by,locator):
        """
        查找到元素并点击
        :param by:
        :param locator:
        :return:
        """
        self.find(by,locator).click()

    def find_send(self,by,locator,value):
        """
        查找到元素并输入
        :param by:
        :param locator:
        :param value:
        :return:
        """
        self.find(by,locator).send_keys(value)

     # 滑动查找元素的方法
    def srcoll1_find(self, text, num=3):
        for i in range(num):
            try:
                return self.driver.find_element(MobileBy.XPATH, f'//*[@text="{text}"]')
            except:
                # 找不到元素就滑动
                wind_size = self.driver.get_window_size()
                width = wind_size['width']
                height = wind_size['height']
                start_x = width / 2
                start_y = height * 0.8
                end_x = start_x
                end_y = height * 0.3
                duration = 3000
                self.driver.swipe(start_x, start_y, end_x, end_y, duration)
            if i == num - 1:
                raise NoSuchElementException(f'找了{num}次，未找到')

    #记录日志
    def log_info(self,msg):
        logging.basicConfig(level=logging.DEBUG,filename="../logs/apitest.log",format="%(asctime)s %(name)s %(levelname)s %(module)s:%(lineno)d %(message)s")
    #     """
    #     添加日志的方法
    #     :return:
    #     """
    #     fileHandler = logging.FileHandler(filename="../logs/apitest.log", encoding="utf-8")
    #     logging.getLogger().setLevel(0)
    #     formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(module)s:%(lineno)d %(message)s')
    #     fileHandler.setFormatter(formatter)
    #     logging.getLogger().addHandler(fileHandler)
    #
        return logging.info(msg)





